package com.woniu.microcommunityserver.book.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.microcommunityserver.activity.entity.ActivityManagementInfoEntity;
import com.woniu.microcommunityserver.book.entity.BookInformationInfoEntity;
import com.woniu.microcommunityserver.book.service.BookInformationInfoService;
import com.woniu.microcommunityserver.res.ResponseMsg;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("book")
public class BookInformationInfoServiceController {
    @Resource
    private BookInformationInfoService bookInformationInfoService;

    @GetMapping("findAll")
    public ResponseMsg findAll(int pageNum){
        PageHelper.startPage(pageNum,10);//这行是重点，表示从pageNum页开始，每页pageSize条数据
        List<BookInformationInfoEntity> books = bookInformationInfoService.findAll();
        PageInfo<BookInformationInfoEntity> pageInfo = new PageInfo<BookInformationInfoEntity>(books);
        return ResponseMsg.builder().code(200).msg("所有图书查询成功！").data(pageInfo).build();
    }

    @GetMapping(value = "findAny")
    public ResponseMsg findByAny(int pageNum,BookInformationInfoEntity bookInformationInfoEntity){
        PageHelper.startPage(pageNum,10);//这行是重点，表示从pageNum页开始，每页pageSize条数据
        Map<String,Object> map =new HashMap<>();
        if (bookInformationInfoEntity.getBookName()==""){
            bookInformationInfoEntity.setBookName(null);
        }
        if (bookInformationInfoEntity.getBookAuthor()==""){
            bookInformationInfoEntity.setBookAuthor(null);
        }
        map.put("bookName",bookInformationInfoEntity.getBookName());
        map.put("bookAuthor",bookInformationInfoEntity.getBookAuthor());
        map.put("bookState",bookInformationInfoEntity.getBookState());
        List<BookInformationInfoEntity> books = bookInformationInfoService.findAny(map);
        PageInfo<BookInformationInfoEntity> pageInfo = new PageInfo<BookInformationInfoEntity>(books);
        return ResponseMsg.builder().code(200).msg("指定图书查询成功！").data(pageInfo).build();
    }

    @PostMapping("insert")
    public ResponseMsg insertBook(@RequestBody BookInformationInfoEntity bookInformationInfoEntity){
        //设置图书数量默认为1
        bookInformationInfoEntity.setBookNumber(1);
        //设置图书状态，新增时，默认为在库
        bookInformationInfoEntity.setBookState(0);
        boolean flag=bookInformationInfoService.insertBook(bookInformationInfoEntity);
        if (flag){
            return ResponseMsg.builder().code(200).msg("新增成功！").build();
        }else {
            return ResponseMsg.builder().code(403).msg("新增失败！").build();
        }
    }

    @DeleteMapping("delete")
    public ResponseMsg deleteBook(int id){
        boolean flag=bookInformationInfoService.deleteBook(id);
        if (flag){
            return ResponseMsg.builder().code(200).msg("删除成功！").build();
        }else {
            return ResponseMsg.builder().code(403).msg("删除失败！").build();
        }
    }

    @PutMapping("loan")
    public ResponseMsg loanBook(int id){
        boolean flag=bookInformationInfoService.loanBook(id);
        if (flag){
            return ResponseMsg.builder().code(200).msg("借出成功！").build();
        }else {
            return ResponseMsg.builder().code(403).msg("借出失败！").build();
        }
    }

    @PutMapping("back")
    public ResponseMsg backBook(int id){
        boolean flag=bookInformationInfoService.backBook(id);
        if (flag){
            return ResponseMsg.builder().code(200).msg("归还成功！").build();
        }else {
            return ResponseMsg.builder().code(403).msg("归还失败！").build();
        }
    }

    @PostMapping(value = "update")
    public ResponseMsg updateBook(@RequestBody BookInformationInfoEntity bookInformationInfoEntity){
        boolean flag=bookInformationInfoService.updateBook(bookInformationInfoEntity);
        if (flag){
            return ResponseMsg.builder().code(200).msg("修改成功！").build();
        }else {
            return ResponseMsg.builder().code(403).msg("修改失败！").build();
        }
    }
}
